
(function($, com) {
	var appkey = 'STVEU8NV8UAEKRI47485NPKY3CBVDIKRKI';
	//var user = app.getState();
	var user = app.getState();
	var uid = user.user_id;
	var myCoinkeydata = 'mycoinlist' + uid;
	var nodedef = localStorage.getItem('nodeurl');
	var nodelist = localStorage.getItem('nodelist');
	//var Web3 = require('web3');
	//var BigNumber = require('bignumber.js');
	//var Ether = new BigNumber(10e+17);
	var providers = ethers.providers;
	//var providers = ethers.providers;
	//nodedef = 'http://47.75.113.31:8045';
	//moneydef = 'http://47.75.113.31:8045';
	//moneydef = 'https://mainnet.infura.io/czBnxy2gmA3a62IcPEzD';
	moneydef = 'https://mainnet.infura.io/czBnxy2gmA3a62IcPEzD';
	//moneydef = 'http://192.168.124.186:8545';
	//nodedef = 'https://api.myetherapi.com/eth';
	nodedef = 'https://mainnet.infura.io/czBnxy2gmA3a62IcPEzD';
	//nodedef = 'http://192.168.124.186:8545';
	//var provider = new providers.JsonRpcProvider(nodedef, 'homestead');
	var ethscankey = 'STVEU8NV8UAEKRI47485NPKY3CBVDIKRKI';
	var ethabiurl = 'https://api.etherscan.io/api?module=contract&action=getabi&apikey=' + ethscankey + '&address=';
	var ethbalanceurl = 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&apikey=' + ethscankey + '&address=';
	var ethcontractbalanceurl = 'https://api.etherscan.io/api?module=account&action=tokenbalance&tag=latest&apikey=' + ethscankey + '&address=';

	var network = providers.networks.homestead;

	com.provider = providers.getDefaultProvider('homestead');
	/*
	var username = "support@ethers.io";
	var password = "password123";
	ethers.Wallet.fromBrainWallet(username, password).then(function(wallet) {
	    console.log("Address: " + wallet.address);
	    // "Address: 0x7Ee9AE2a2eAF3F0df8D323d555479be562ac4905"
	});*/
	/*
	if(!nodedef) {
		$.getJSON(nodeurl, function(res) {
			localStorage.setItem('nodelist', JSON.stringify(res));
			for(var p in res['eth']) {
				nodedef = p;
				break;
			}
			localStorage.setItem('nodeurl', nodedef);
		});
	} else {
		//nodedef = 'https://api.myetherapi.com/eth';
		//com.provider = new providers.JsonRpcProvider(nodedef, network);
	}
	com.getnode = function() {
		return localStorage.getItem('nodeurl');
	}*/
	com.refreshlist = function() {
		var mainWin = plus.webview.getWebviewById('coin.html');
		mui.fire(mainWin, 'flashWallet', null);
	}
	//com.web3 = new Web3(new Web3.providers.HttpProvider(nodedef));
	//console.log(nodedef);
	//console.log(apiurl + 'coin/getlist');
	/**
	 * 根据钱包类型获取用户的钱包
	 * @param {Object} type
	 */
	//localStorage.getItem('mywallet' + uid + 'eth');
	com.getWallet = function(type) {
		if(!type || type == 'ethcontact') {
			type = 'eth';
		}
		var data = localStorage.getItem('mywallet' + uid + type);
		if(data) {
			return JSON.parse(data);
		} else {
			return false;
		}
	};
	/**
	 * 根据uid获取用户的钱包地址
	 * @param {Object} uid
	 * @param {Object} etype
	 * @param {Object} func
	 */
	com.getAddress = function(uid, etype, func) {
		if(etype == 'ethcontact') {
			etype = 'eth';
		}
		$.getJSON(apiurl + 'token/getaddress?uid=' + uid + '&etype=' + etype, func);
	}
	/**
	 * 获取币种数据
	 * @param {Object} token
	 */
	com.getCoindata = function(token) {
		var coinlist = {};
		com.getmyCoin(function(data) {
			coinlist = data;
		});
		coindata = coinlist[token];
		if(!coindata) {
			com.getlist(function(data) {
				coindata = data[token];
				coinlist[token] = coindata;
				localStorage.setItem('mycoinlist' + uid, JSON.stringify(coinlist));
				com.refreshlist();
			});
		}
		var ctype = coindata['ctype'];

		if(ctype == 'ethcontact') {
			coindata['abi'] = com.getabi(token);
		}
		return coindata;
	}
	/**
	 * 校验用户的金额是否可支付
	 * @param {Object} money
	 * @param {Object} coindata
	 * @param {Object} func
	 */
	com.checkPayMoney = function(money, coindata, func) {
		if(coindata['ctype'] == 'eth') {
			com.checkEthAmount(money, func);
		} else if(coindata['ctype'] == 'ethcontact') {
			com.checkEthContactAmount(money, coindata, func);
		} else if(coindata['ctype'] == 'btc') {
			com.checkBtcAmount(money, coindata, func);
		} else {
			$.alert('开发中！');
			return;
		}
	}
	/**
	 * 获取系统币金额
	 * @param {Object} func
	 */
	com.getSysAmount = function(func) {
		var coindata = com.getDefCoin();
		com.getAmount(coindata, func);
	}
	/**
	 * 获取用户金额
	 * @param {Object} coindata
	 * @param {Object} func
	 */
	com.getAmount = function(coindata, func) {
		var ctype = coindata['ctype'];
		if(ctype == 'eth') {
			com.getEthAmount(func);
		} else if(ctype == 'ethcontact') {
			com.getEthContactAmount(coindata, func);
		} else if(ctype == 'btc') {
			var mywallet = com.getWallet('btc');
			if(bitscan) {
				bitscan.getBalance(mywallet.address, function(res) {
					func(res.money, mywallet.address);
				});
			}
		} else {
			return;
		}
	}
	/**
	 * 获取用户的以太坊金额
	 * @param {Object} func
	 */
	com.getEthAmount = function(func) {
		var myWallet = com.getWallet('eth');
		//console.log(JSON.stringify(myWallet));
		//console.log(ethbalanceurl + myWallet.address);
		/*
		$.getJSON(ethbalanceurl + myWallet.address, function(res) {
			console.log(JSON.stringify(res));
			if(res && res.status > 0) {
				var etherString = ethers.utils.formatEther(res.result);
				func(etherString, myWallet.address);
				//func(res.result/12, myWallet.address);
			} else {
				func(0, myWallet.address);
			}
		});*/
		//plus.nativeUI.showWaiting("钱包校验中...");
		
		var providers = ethers.providers;
		var provider = new providers.JsonRpcProvider(moneydef, 'homestead');
		provider.getBalance(myWallet.address).then(function(balance) {
			var etherString = ethers.utils.formatEther(balance);
			func(etherString, myWallet.address);

		});
	}
	/**
	 * 获取用户的合约金额
	 * @param {Object} coindata
	 * @param {Object} func
	 */
	com.getEthContactAmount = function(coindata, func) {
		var myWallet = com.getWallet('eth');
		/*
		$.getJSON(ethcontractbalanceurl + myWallet.address + '&contractaddress=' + coindata['address'], function(res) {
			if(res && res.status > 0) {
				//func(res.result, myWallet.address);
				var etherString = ethers.utils.formatEther(res.result);
				func(etherString, myWallet.address);
			} else {
				func(0, myWallet.address);
			}
		});
		//var providers = ethers.providers;
		//var provider = providers.getDefaultProvider('homestead');
		*/
		var providers = ethers.providers;
		var provider = new providers.JsonRpcProvider(moneydef, 'homestead');
		var contracts = new ethers.Contract(coindata['address'], coindata['abi'], provider);
		contracts.balanceOf(myWallet.address).then(function(res) {
			//item['money'] = res["_bn"];
			var contractsetherString = ethers.utils.formatEther(res);
			func(contractsetherString, myWallet.address);

		});
	}
	com.checkEthAmount = function(money, func) {
		var myWallet = com.getWallet('eth');
		/*
		$.getJSON(ethbalanceurl + myWallet.address, function(res) {
			console.log(JSON.stringify(res));
			if(res && res.status > 0) {
				var etherString = ethers.utils.formatEther(res.result);
				//func(etherString, myWallet.address);
				//func(res.result/12, myWallet.address);
				if(etherString < 0.001) {
					$.alert("您至少要持有0.001ETH来保证矿工费支付");

					return;
				}
				if(money * 1 > etherString * 1) {
					$.alert("ETH账户余额不足");
					return;
				}
				func();
			} else {
				return;
			}
		});
		*/
		plus.nativeUI.showWaiting("钱包校验中...");
		var providers = ethers.providers;
		//var provider = providers.getDefaultProvider('homestead');
		var provider = new providers.JsonRpcProvider(moneydef, 'homestead');
		provider.getBalance(myWallet.address).then(function(balance) {
			var etherString = ethers.utils.formatEther(balance);
			plus.nativeUI.closeWaiting();
			if(etherString < 0.001) {
				$.alert("您至少要持有0.001ETH来保证矿工费支付");

				return;
			}
			if(money * 1 > etherString * 1) {
				$.alert("ETH账户余额不足");
				return;
			}
			func();
		});
	}
	com.checkEthContactAmount = function(money, coindata, func) {
		var myWallet = com.getWallet('eth');
		/*
		$.getJSON(ethcontractbalanceurl + myWallet.address + '&contractaddress=' + coindata['address'], function(res) {
			if(res && res.status > 0) {
				//func(res.result, myWallet.address);
				var contractsetherString = ethers.utils.formatEther(res.result);
				if(money * 1 > contractsetherString * 1) {
					console.log(contractsetherString);
					$.alert(coindata['name'] + "账户余额不足");

					return;
				}
				func();
			} else {
				return;
			}
		});
		*/
		var mywallet = com.getWallet('eth');
		plus.nativeUI.showWaiting("钱包校验中...");
		var providers = ethers.providers;
		//var provider = providers.getDefaultProvider('homestead');
		var provider = new providers.JsonRpcProvider(moneydef, 'homestead');
		provider.getBalance(mywallet.address).then(function(balance) {
			var etherString = ethers.utils.formatEther(balance);
			//console.log(etherString);
			//return;
			//if(ctype == 'ethcontact') {
			if(etherString < 0.001) {
				$.alert("您至少要持有0.001ETH来保证矿工费支付");
				plus.nativeUI.closeWaiting();
				return;
			}
			var contracts = new ethers.Contract(coindata['address'], coindata['abi'], provider);
			contracts.balanceOf(mywallet.address).then(function(res) {
				//item['money'] = res["_bn"];
				var contractsetherString = ethers.utils.formatEther(res);
				plus.nativeUI.closeWaiting();
				if(money * 1 > contractsetherString * 1) {
					console.log(contractsetherString);
					$.alert(coindata['name'] + "账户余额不足");

					return;
				}
				func();
			});

		});
	}
	com.checkBtcAmount = function(money, coindata, func) {
		var mywallet = com.getWallet('btc');
		plus.nativeUI.showWaiting("钱包校验中...");

	}
	com.payeth = function(toaddr, money, sucess, errorfunc) {
		com.checkEthAmount(money, function() {
			com.sendeth(toaddr, money, sucess, errorfunc);
		});
	}
	com.sendeth = function(toaddr, money, sucess, errorfunc) {
		var mywallet = com.getWallet('eth');
		var wallet = new ethers.Wallet(mywallet.privkey);
		//wallet.provider = ethers.providers.getDefaultProvider();
		wallet.provider = new providers.JsonRpcProvider(nodedef, 'homestead');
		var amount = ethers.utils.parseEther(money.toString());
		var sendPromise = wallet.send(toaddr, amount);
		sendPromise.then(function(transactionHash) {
			console.log(JSON.stringify(transactionHash));
		});
		var options = {
			gasLimit: 80000
		};
		var promiseSend = wallet.send(toaddr, amount, options);
		//plus.nativeUI.closeWaiting();
		promiseSend.then(function(transaction) {
			sucess(transaction);
		});
		promiseSend.catch(function(error) {
			errorfunc(error);
		});
	}
	com.payToAddrEth = function(address, ctype, coindata, money, sucess, errfunc) {
		if(ctype == 'eth') {
			com.payeth(address, money, function(obj) {
				console.log(JSON.stringify(obj));
				if(obj.hash) {
					sucess(obj, address);
				} else {
					//$.toast('支付失败！');
					errfunc(obj);
				}
			}, function(err) {
				//$.toast('支付失败！');
				errfunc(err);
			});
		} else {
			com.payethcontact(address, money, coindata, function(obj) {
				//console.log(JSON.stringify(obj));
				if(obj.hash) {
					sucess(obj, address);
				} else {
					//$.toast('支付失败！');
					errfunc(obj);
				}
			}, function(err) {
				//$.toast('支付失败！');
				errfunc(err);
			});
		}
	}
	/**
	 * 付款给系统 带密码校验
	 * @param {Object} money
	 * @param {Object} sucess
	 * @param {Object} err
	 */
	com.payToSystem = function(money, sucess, err) {
		var coindata = com.getDefCoin();
		var ctype = coindata['ctype'];
		//var mywallet = com.getWallet(ctype);

		return com.payToAddrByPwd(sysethaddr, ctype, coindata, money, sucess, err);
	}
	/**
	 * 付款给某个地址，带密码校验
	 * @param {Object} address
	 * @param {Object} ctype
	 * @param {Object} coindata
	 * @param {Object} money
	 * @param {Object} sucess
	 * @param {Object} err
	 */
	com.payToAddrByPwd = function(address, ctype, coindata, money, sucess, errfunc) {
		ethscan.checkPwd(ctype, function() {
			if(ctype == 'btc') {
				$.toast('开发中！');
				return;
			} else {
				ethscan.payToAddrEth(address, ctype, coindata, money, function(obj, toaddr) {
					sucess(obj.hash, toaddr);
				}, function(err) {
					console.log(err);
					errfunc(err);
				});

				return;
			}
		}, function() {
			$.alert("您还没有设定默认钱包");
			$.openWindow({
				url: "mgrwallet.html",
				id: "mgywallet"
			});
			return;
		});
	}
	com.payToUserEth = function(tid, ctype, coindata, money, sucess, err) {
		com.getAddress(tid, ctype, function(rt) {
			if(rt.status && rt.address) {
				com.payToAddrEth(rt.address, ctype, coindata, money, sucess, err);
			} else {
				$.alert('对方没有创建钱包！');
				err(rt);
				return;
			}
		});

	}
	com.checklasthash = function(func) {
		$.getJSON(apiurl + 'trade/getlasthash?uid=' + uid, function(res) {
			//console.log(JSON.stringify(res));
			if(res.status && res.status > 0) {
				com.checkcanTrade(res.hash, func);
			}
		});
	}
	com.checkcanTrade = function(tohash, func) {
		//取用户交易的最后一个hash
		//var url = 'https://api.etherscan.io/api?module=account&action=txlist&address=' + addr + '&startblock=0&endblock=99999999&page=1&offset=1&sort=desc&apikey=STVEU8NV8UAEKRI47485NPKY3CBVDIKRKI';
		//$.getJSON(apiurl + 'trade/getlasthash?uid=' + uid, function(res) {
		//console.log(JSON.stringify(res));
		//if(res.status && res.status > 0) {
		//var tohash = res.hash;
		//console.log(hash);
		if(!tohash) {
			func({
				status: 0
			});
		}
		var providers = ethers.providers;
		//var provider = providers.getDefaultProvider();
		var provider = new providers.JsonRpcProvider(nodedef, 'homestead');
		provider.getTransaction(tohash).then(function(transaction) {
			console.log(JSON.stringify(transaction));

			if(!transaction) {
				//$.alert();
				func({
					status: 0,
					'msg': '有一笔交易发送失败！'
				});
				//plus.webview.currentWebview().close();
				return;
			}
			if(!transaction.blockNumber) {
				transaction.status = 2;
				transaction.msg = '您有一笔交易正在打包中，可能会影响您的交易！';
				func(transaction);
				//plus.webview.currentWebview().close();
				return;
			}

			provider.getTransactionReceipt(tohash).then(function(transactionReceipt) {
				if(!transactionReceipt) {
					//$.alert();
					//plus.webview.currentWebview().close();
					func({
						status: 3,
						msg: '您有一笔交易正在接收中，可能会影响您的交易！'
					});
					return;
				}
			});
			func({
				status: 0
			});
		});
		//}
		//});
	}
	com.checkPwd = function(etype, func, err) {
		if(etype == 'ethcontact') {
			etype = 'eth';
		}
		var mywallet = com.getWallet(etype);
		if(!mywallet || !mywallet.address) {
			err();
			return;
		}
		var btnArray = ['确定', '取消'];
		mui.prompt('请输入钱包密码', '', '', btnArray, function(e) {
			if(e.index == 0) {
				if(e.value && e.value.length > 0) {
					if(md5(e.value) != mywallet.passwd) {
						$.toast("密码错误！");
						return;
					} else {
						func();
					}
				} else {
					$.toast("密码不能为空！");
				}
			}
		}, 'div').element.querySelector('input').type = 'password';

	}
	com.payethcontact = function(toaddr, money, coindata, sucess, errorfunc) {
		com.checkEthContactAmount(money, coindata, function() {
			com.sendethcontact(toaddr, money, coindata, sucess, errorfunc);
		});
	}
	com.sendethcontact = function(toaddr, money, coindata, sucess, errorfunc) {
		var myWallet = com.getWallet('eth');
		var providers = ethers.providers;
		//var provider = providers.getDefaultProvider('homestead');
		var provider = new providers.JsonRpcProvider(nodedef, 'homestead');
		var wallet = new ethers.Wallet(myWallet.privkey, provider);
		var contract = new ethers.Contract(coindata['address'], coindata['abi'], wallet);

		var options = {
			//gasPrice: 1100000000, // 1.1Gwei
			gasLimit: 80000
		};
		//console.log(money);
		var amount = ethers.utils.parseEther(money.toString());
		//console.log(amount);
		//return;
		//plus.nativeUI.closeWaiting();
		var callPromise = contract.transfer(toaddr, amount, options);
		callPromise.then(function(obj) {
			console.log(JSON.stringify(obj));
			//$.toast('支付成功，打包需要3-5分钟！');
			sucess(obj);
		})

		callPromise.catch(function(error) {
			//console.log(JSON.stringify(error));
			errorfunc(error);
		});
	}
	com.setWallet = function(data, type) {
		localStorage.setItem('mywallet' + uid + type, JSON.stringify(data));
		var postdata = {
			uid: uid,
			address: data.address,
			etype: type
		};
		//console.log(JSON.stringify(postdata));
		$.post(apiurl + 'token/addwallet', postdata, function(res) {
			//console.log(JSON.stringify(res));
		});
	}
	com.addWallet = function(add, type) {
		var type = type || 'eth';
		var data = localStorage.getItem('mywallet' + uid + type);
		if(!data) {
			data = {};
		} else {
			data = JSON.parse(data);
		}
		for(var p in add) {
			data[p] = add[p];
		}
		if(!data['list']) {
			data['list'] = {};
		}
		data['list'][add['address']] = add;
		com.setWallet(data, type);
		//localStorage.setItem('mywallet' + uid + type, JSON.stringify(data));
	};
	com.editWallet = function(field, val, type) {
		var type = type || 'eth';
		var data = com.getWallet(type);

		var list = data['list'];
		var addr = data['address'];
		var def = list[addr];
		//console.log(field);
		//console.log(val);
		//return;
		data[field] = val;
		def[field] = val;
		data['list'][addr] = def;
		com.setWallet(data, type);
		//localStorage.setItem('mywallet' + uid + type, JSON.stringify(data));
	};
	com.getlist = function(func) {
		//plus.storage.removeItem('coinlist');
		var listdata = localStorage.getItem('coinlist');
		if(!listdata) {
			$.getJSON(apiurl + 'coin/getlist', function(res) {
				//$.getJSON('coinlist.json', function(res) {
				//console.log(res);
				localStorage.setItem('coinlist', JSON.stringify(res));
				return func(res);
			});
		} else {
			return func(JSON.parse(listdata));
		}
	};
	com.getprice = function(coins, func) {
		$.getJSON(apiurl + 'eth/get_price?coin=' + coins, function(res) {
			//console.log(JSON.stringify(res));
			//plus.storage.setItem('coinlist', JSON.stringify(res));
			func(res);
		});
	}
	com.getpriceList = function() {
		pricelist = localStorage.getItem('coinprice');
		return JSON.parse(pricelist);
	}
	com.getDefprice = function() {
		var coindata = com.getDefCoin();
		var pricelist = com.getpriceList();
		var coinname = coindata['name'];
		return pricelist[coinname];
	}
	com.getCoinlist = function() {
		var listdata = localStorage.getItem('coinlist');
		return JSON.parse(listdata);
	}
	com.getDefCoin = function() {
		var coinlist = com.getCoinlist();
		if(coinlist) {
			var def = coinlist[defaultCoinAddr];
			def['abi'] = com.getabi(defaultCoinAddr);
			return def;
		} else {
			return com.getlist(function(data) {
				var def = coinlist[defaultCoinAddr];
				def['abi'] = com.getabi(defaultCoinAddr);
				return def;
			});
		}

	}
	com.set_ethBalance = function(user) {

		var address = user.eth_address;

		com.provider.getBalance(address).then(function(balance) {
			var etherString = ethers.utils.formatEther(balance);
			//console.log(etherString);
			user.eth_money = etherString;
			app.setState(user);
			//doc.getElementById("eth_money").innerHTML = etherString;
		});

	}
	com.get_ethBalance = function(isnew) {
		var user = app.getState();
		if(!isnew) {
			return user.eth_money;
		} else {
			com.set_ethBalance(user);
			return user.eth_money;
		}

	}
	com.setEth = function() {
		//console.log(user.eth_privkey);
		if(!user.eth_address) {
			/*
							var wallet = ethers.Wallet.createRandom();
							//console.log(JSON.stringify(wallet));
							wallet.uid = user.user_id;
							wallet.etype = 'eth';
			
							var str = apiurl + 'token/getwallet?';
							for(var p in wallet){
								if(p != 'sign'){
									str += p + '='+wallet[p] + '&';
								}
								
							}
							//console.log(JSON.stringify(str));
							$.post(apiurl + 'token/getwallet', wallet, function(res) {
								console.log(JSON.stringify(res));
								user.eth_address = res.address;
								user.eth_privkey = res.privkey;
								user.eth_walletname = res.name;
								app.setState(user);
								//com.set_ethBalance(user);
							});*/
		} else {
			//com.set_ethBalance(user);
		}
	}

	function initcoinlist(listdata) {
		listdata = listdata ? listdata : {};
		var list = {};
		for(var p in listdata) {
			var item = listdata[p];
			/*
			if(p == 'eth') {
				item['money'] = com.get_ethBalance();
			} else {
				//console.log(item['abi']);
				console.log(item['address']);
				//var provider = ethers.providers.getDefaultProvider('homestead');
				//if(item['id'] == 88) {
				var contracts = new ethers.Contract(item['address'], item['abi'], com.provider);

				contracts.balanceOf(user.eth_address).then(function(res) {
					//item['money'] = res["_bn"];
					var etherString = ethers.utils.formatEther(res).toFixed(3);
					console.log(JSON.stringify(etherString));
				});
				//}
				//var contracts = new ethers.Contract(item['address'], item['abi'], com.provider);

			}*/
			//console.log(JSON.stringify(item));
			item['img'] = serverurl + item['logo'];
			list[p] = item;
		}
		return list;
	}
	com.getmycoinlist = function() {
		var mycoin;
		com.getmyCoin(function(data) {
			mycoin = data;
		});
		return mycoin;
	}
	com.getmyCoin = function(func) {

		var listdata = localStorage.getItem(myCoinkeydata);
		/*
		if(!listdata) {
			$.getJSON(apiurl + 'eth/getUserCoinList?uid=' + uid, function(res) {
				res = initcoinlist(res);
				localStorage.setItem(myCoinkeydata, JSON.stringify(res));
				func(res);
			});
		} else {
			var data = JSON.parse(listdata);
			func(initcoinlist(data));
		}*/
		if(!listdata) {
			com.getlist(function(data) {
				listdata = {};
				listdata['eth'] = data['eth'];
				listdata[defaultCoinAddr] = data[defaultCoinAddr];
				localStorage.setItem(myCoinkeydata, JSON.stringify(listdata));
				func(initcoinlist(listdata));
				com.getabi(defaultCoinAddr);
			});
		} else {
			var data = JSON.parse(listdata);
			func(initcoinlist(data));
		}
	};
	com.clear = function() {
		com.getmyCoin(function(item) {
			for(var p in item) {
				var abikey = p + 'abi';
				localStorage.removeItem(abikey);
			}
		});
		localStorage.removeItem(myCoinkeydata);
		localStorage.removeItem('coinlist');
	}
	com.getabi = function(addr) {
		var abikey = addr + 'abi';
		var data = localStorage.getItem(abikey);
		if(!data) {
			$.getJSON(apiurl + 'coin/getabi?addr=' + addr, function(res) {
				if(res.abi) {
					localStorage.setItem(abikey, res.abi);
				}

			});
		}
		return data;
	}
	com.addMyCoin = function(addr) {
		var listdata = localStorage.getItem(myCoinkeydata);
		var alllist = com.getCoinlist();
		if(!listdata[addr]) {
			listdata[addr] = alllist[addr];
		}
		localStorage.setItem(myCoinkeydata, JSON.stringify(listdata));
		com.getabi(addr);
	}
	com.setmyCoin = function(el, func) {
		var address = el.id;

		var listdata = localStorage.getItem(myCoinkeydata);
		var listdata = listdata ? JSON.parse(listdata) : {};
		var alllist = com.getCoinlist();
		if(!listdata[address]) {
			listdata[address] = alllist[address];
		} else {
			delete listdata[address];
		}
		localStorage.setItem(myCoinkeydata, JSON.stringify(listdata));
		func(listdata);
		com.getabi(address);
		/*
		$.post(apiurl + 'eth/setUserCoinList', postdata, function(res) {
			res = initcoinlist(res);
			var setdata = JSON.stringify(res);
			console.log(setdata);
			//localStorage.setItem(myCoinkeydata, setdata);
			func(res);
		});*/
	}
	//com.getlist();
	/*
	com.getpricelist = function(func){
		var coinprice = localStorage.getItem("coinprice");
		var pricedata = JSON.parse(coinprice) || {};
		var now = app.now();
		var lasttime = pricedata.lasttime || 0;
		if(now - pricedata > 3600){
			var url = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LTC,ETC,DOGE,EOS&tsyms=USD';
			$.getJSON(url, function(res){
				for(var p in res) {
					pricedata[p] = res[p]['USD'];
				}
				pricedata.lasttime = now;
				//console.log(JSON.stringify(pricedata));
				localStorage.setItem(JSON.stringify(pricedata));
				func(pricedata);
			});
		}else{
			func(pricedata);
		}
		return pricedata;	
	}
	com.balance = function(address){
		return 'https://api.etherscan.io/api?module=account&action=balance&address='+address+'&tag=latest&apikey=' + appkey;
	}
	com.txlistinternal = function(addr, page) {
		return 'https://api.etherscan.io/api?module=account&action=txlistinternal&address='+address+'&startblock=0&endblock=2702578&page='+page+'&offset=20&sort=asc&apikey=' + appkey;
	}
	com.abi = function(address){
		return 'https://api.etherscan.io/api?module=contract&action=getabi&address='+address+'&apikey=' + appkey;
	}
	com.contactbalance = function(address,ctaddress) {
		return 'https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress='+ctaddress+'&address='+address+'&tag=latest&apikey=' + appkey;
	}*/
}(mui, window.ethscan = {}));

